package com.house.pc;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import java.io.IOException;

/**
 * @author 小贾
 * @version 1.0
 * @description: TODO
 * @date 2025/9/16 9:02
 */
public class AnjukeHouseSpider {
    public static void main(String[] args) {
        String url = "https://hf.anjuke.com/sale/zhengwuqu/p2"; // 示例列表页 URL，可替换为真实分页链接

        try {
            Document doc = Jsoup.connect(url)
                    .userAgent("Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/114.0.0.0 Safari/537.36")
                    .timeout(30000)
                    .get();

            Elements propertyList = doc.select("div.property[data-v-5369c928]");

            for (Element property : propertyList) {
                String title = property.select("h3.property-content-title-name").text();
                String info = property.select("p.property-content-info-attribute").text(); // 户型
                String area = property.select("p.property-content-info-text").get(1).text(); // 面积
                String orientation = property.select("p.property-content-info-text").get(2).text(); // 朝向
                String floor = property.select("p.property-content-info-text").get(3).text(); // 楼层
                String year = property.select("p.property-content-info-text").get(4).text(); // 建造年份
                String community = property.select("p.property-content-info-comm-name").text();
                String address = property.select("p.property-content-info-comm-address span").text();
                String totalPrice = property.select("p.property-price-total-num").text();
                String avgPrice = property.select("p.property-price-average").text();

                System.out.println("标题: " + title);
                System.out.println("户型: " + info);
                System.out.println("面积: " + area);
                System.out.println("朝向: " + orientation);
                System.out.println("楼层: " + floor);
                System.out.println("建造年份: " + year);
                System.out.println("小区: " + community);
                System.out.println("地址: " + address);
                System.out.println("总价: " + totalPrice + "万");
                System.out.println("单价: " + avgPrice);
                System.out.println("----------------------");
            }

        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
